package com.netdisk.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.netdisk.dao.Dao;
import com.netdisk.entity.Company;

/**
 * 公司dao类
 * 
 * @author chenlong 2013-4-13
 */
@Repository(value = "companyDao")
public class CompanyDao extends Dao<Company> {

	@Override
	public void save(Company obj) {
		Session session = sessionFactory.getCurrentSession();
		session.save(obj);
	}

	@Override
	public void update(Company obj) {
		Session session = sessionFactory.getCurrentSession();
		session.update(obj);
	}

	@Override
	public void delete(Company obj) {
		Session session = sessionFactory.getCurrentSession();
		session.delete(obj);
	}

	@Override
	public List<Company> findAll() {
		Session session = sessionFactory.getCurrentSession();
		List<Company> companys = null;
		companys = session.createQuery("from Company").list();
		return companys;
	}

	@Override
	public Company findById(String id) {
		Session session = sessionFactory.getCurrentSession();
		List<Company> companys = null;
		Company company = null;
		Query query = session.createQuery("from Company where id=:id");
		query.setString("id", id);
		companys = query.list();
		if (!companys.isEmpty()) {
			company = companys.get(0);
		}
		return company;
	}

	@Override
	public List<Company> findByTop(int from, int max) {
		Session session = sessionFactory.getCurrentSession();
		List<Company> companys = null;
		Query query = session.createQuery("from Company");
		query.setFirstResult(from);
		query.setMaxResults(max);
		companys = query.list();
		return companys;
	} 

}
